﻿using System.Collections.Generic;
using System.Text;
using com.eforcegloabal.crux.web.wrokflows;
using com.eforceglobal.crux.web.wrokflows;

public class PurchaseOrderFacade
{
    public bool AddPurchaseOrderCreation(PurchaseOrderInfo purchaseOrder, string currentUser) //PurchaseOrderCreation Activity
    {
        bool succes = true;
        WorkflowManager wfManager = new WorkflowManager();
        purchaseOrder.WorkflowInstanceId = wfManager.CreateWorkflowInstance("Inventory",
                                                                                        "PurchaseOrderCreation",
                                                                                        "SavePuchaseOrder",
                                                                                        "PurchaseOrderCreation",
                                                                                       "cc", currentUser, null);

        wfManager.SetAttribute(purchaseOrder.WorkflowInstanceId, GetAttributesForDb(purchaseOrder));
        new Queries().InsertPurchaseOrder(purchaseOrder);
        wfManager.FireActionOnWorkflowInstance(purchaseOrder.WorkflowInstanceId, "SavePurchaseOrder", currentUser, null, true);
        return succes;
    }
    private List<DbAttribute> GetAttributesForDb(PurchaseOrderInfo purchaseOrder)
    {
        List<DbAttribute> dbAttributes = new List<DbAttribute>();
        dbAttributes.Add(new DbAttribute { Name = "ItemName", Value = purchaseOrder.ItemName });
        dbAttributes.Add(new DbAttribute { Name = "ItemQuantity", Value = purchaseOrder.Itemquantity.ToString() });
        dbAttributes.Add(new DbAttribute { Name = "Price", Value = purchaseOrder.Price.ToString() });
        dbAttributes.Add(new DbAttribute { Name = "PurchaseOrderDate", Value = purchaseOrder.PurchaseOrderDate.ToString() });
        dbAttributes.Add(new DbAttribute { Name = "IsCredit", Value = purchaseOrder.IsCredit.ToString() });
        return dbAttributes;
    }

    public bool SubmitPurchaseOrder(PurchaseOrderInfo purchaseOrder, string currentUser) //Order Creation Activity
    {
        bool succes = true;
        WorkflowManager wfManager = new WorkflowManager();

        wfManager.SetAttribute(purchaseOrder.WorkflowInstanceId, GetAttributesForDb(purchaseOrder));
        new Queries().ModifyPurchaseOrder(purchaseOrder);
        wfManager.FireActionOnWorkflowInstance(purchaseOrder.WorkflowInstanceId, "SubmitPurchaseOrder", currentUser, null, true);
        return succes;
    }
    public bool RejectedFromROLValidation(PurchaseOrderInfo purchaseOrder, string currentUser) //ROL Activity
    {
        bool succes = true;
        WorkflowManager wfManager = new WorkflowManager();

        //wfManager.SetAttribute(purchaseOrder.WorkflowInstanceId, GetXMLAttribute(purchaseOrder));
        //new Queries().ModifyPurchaseOrder(purchaseOrder);
        wfManager.FireActionOnWorkflowInstance(purchaseOrder.WorkflowInstanceId, "Reject", currentUser, null, true);
        return succes;
    }
    public bool SubmitPurchaseQuantityRequest(PurchaseOrderInfo purchaseOrder, string currentUser) //ROL Activity
    {
        bool succes = true;
        WorkflowManager wfManager = new WorkflowManager();

        wfManager.SetAttribute(purchaseOrder.WorkflowInstanceId, GetAttributesForDb(purchaseOrder));
        new Queries().ModifyPurchaseOrder(purchaseOrder);
        wfManager.FireActionOnWorkflowInstance(purchaseOrder.WorkflowInstanceId, "SubmitPurchaseQuantityRequest", currentUser, null, true);
        return succes;
    }
    public bool RejectedFromPurchaseOrderCompleted(PurchaseOrderInfo purchaseOrder, string currentUser) //PurchaseOrderSentToVendor Activity
    {
        bool succes = true;
        WorkflowManager wfManager = new WorkflowManager();

        //wfManager.SetAttribute(purchaseOrder.WorkflowInstanceId, GetXMLAttribute(purchaseOrder));
        //new Queries().ModifyPurchaseOrder(purchaseOrder);
        wfManager.FireActionOnWorkflowInstance(purchaseOrder.WorkflowInstanceId, "Reject", currentUser, null, true);
        return succes;
    }
    public bool AddPurchaseOrderCompleted(PurchaseOrderInfo purchaseOrder, string currentUser) //PurchaseOrderSentToVendor Activity
    {
        bool succes = true;
        WorkflowManager wfManager = new WorkflowManager();

        wfManager.SetAttribute(purchaseOrder.WorkflowInstanceId, GetAttributesForDb(purchaseOrder));
        new Queries().ModifyPurchaseOrder(purchaseOrder);
        wfManager.FireActionOnWorkflowInstance(purchaseOrder.WorkflowInstanceId, "PurchaseOrderCompleted", currentUser, null, true);
        return succes;
    }


    //private string GetXMLAttribute(PurchaseOrderInfo purchaseOrder)
    //{
    //    StringBuilder sb = new StringBuilder();
    //    sb.Append("<DBAttributes>");
    //    sb.Append("<DBAttribute Name='ItemQuantity' Value='" + purchaseOrder.Itemquantity + "' />");
    //    sb.Append("<DBAttribute Name='ItemName' Value='" + purchaseOrder.ItemName + "' />");
    //    sb.Append("<DBAttribute Name='PurchaseOrderDate' Value='" + purchaseOrder.PurchaseOrderDate + "' />");
    //    sb.Append("<DBAttribute Name='Price' Value='" + purchaseOrder.Price + "' />");
    //    sb.Append("<DBAttribute Name='IsCredit' Value='" + purchaseOrder.IsCredit + "' />");
    //    sb.Append("</DBAttributes>");
    //    return sb.ToString();
    //}
    public List<PurchaseOrderInfo> PurchaseOrders()
    {
        return new Queries().GetPurchaseOrderInfo();
    }
    public List<PurchaseOrderInfo> PurchaseOrdersByStateName(string stateName)
    {
        return new Queries().GetPurchaseOrderInfoByStateName(stateName);
    }
    public PurchaseOrderInfo PurchaseOrdersByWorkFlowInstanceId(int instId)
    {
        return new Queries().PurchaseOrdersByWorkFlowInstanceId(instId);
    }

}
